<?php
namespace Home\Controller;
use Think\Controller;

  
 
class MyAccountController extends Controller {
            
	public function index(){
	
	
		if($_GET['session']){
            /*金币兑换*/		
			$UserId = getUserId();
			session('chatCountId',$UserId);
		}else{
			$UserId = session('chatCountId');
		}
		    $money = M('tbuseraccount')->field('nmoney,ngiftmoney')->where("nuserid=$UserId")->select();
			$this->assign('giftmoney',$money[0]['ngiftmoney']);
			$this->assign('money',$money[0]['nmoney']);
			$this->display();
		
		}
		

	public function oper(){
		if($_POST)
		{	 
			 $money=I('post.money',0,'int');
			 $UserId = session('chatCountId'); 
			 $C = M('tbuseraccount');
			 $C->startTrans(); 
			 $i=$C->where("nuserid=$UserId")->setInc('nmoney',$money);
			 $k=$C->where("nuserid=$UserId")->setDec('ngiftmoney',$money);
		 
			if(!empty($i)&&!empty($k))
			{	//金币兑换增加流水
				$userMoney = $C ->field('nmoney,ngiftmoney')->where("nuserid=$UserId")->select();
				$data['ntype']=2;
				$gloden = $userMoney[0]['nmoney'];
				$data['nmoney']= $money;
				$data['dtime']=date('Y-m-d H:i:s');
				$data['nuserid'] = $UserId;
				$data['nbalance'] = $gloden;
				$data['cdesc'] = "礼金兑换"; 
				$r=M('tbusermoneyflow')->add($data);
				//礼金减少兑换流水
				$gift=$userMoney[0]['ngiftmoney'];
				$data['nmoney']= -$money;
				$data['nbalance'] =$gift; 
				$j=M('tbusergiftmoneyflow')->add($data);	
				if($r&&$j){
					$C->commit();
					echo 1;
				
					}else{
						$C->rollback();
					echo 2; 
					}
			}						
		}		
	}
	

	public function navi(){
			$UserId = session('chatCountId');
	
			$preSevenDays = strtotime("-2 Days");			
			$startTime = date("Y-m-d", $preSevenDays); 				
			$endTime = date("Y-m-d",strtotime('+1 day'));
			$field = 'a.nuserid,a.nbuddyid,a.cgiftname,a.ncount,a.dtime,b.calias';
			$G = M('tbusertradegift');
            if($UserId){
			/*收到的礼物*/
			    if(I('post.cate')=='receive'){
						$join = 'a LEFT JOIN tbuserinformation b ON a.nuserid = b.nuserid';				
						$where = "a.nbuddyid = $UserId AND a.dtime BETWEEN '$startTime' AND '$endTime'";	
						
				}
				else if(I('post.cate')=='send'){
					/*送出的礼物*/
						$join = 'a LEFT JOIN tbuserinformation b ON a.nbuddyid = b.nuserid';
						$where = " a.nuserid = $UserId AND a.dtime BETWEEN '$startTime' AND '$endTime'";
					    }
					$page =I('post.page');
                    $pageSize=5;//显示的几条数据
                    $startPage = ($page-1)*$pageSize;					
					$data = $G->join($join)
							   ->field($field)
							   ->where($where)
							   ->limit($startPage,$pageSize)
							   ->order('dtime DESC')
							   ->select();
					
					$count = $G->join($join)					 
								->where($where)
								->count();//总记录
					
					
					$totalPage = ceil($count/$pageSize);//总页数	

					if($totalPage==0){
					  $this->ajaxReturn(0);
					}else{
					foreach($data as $key=>$val){
						$data[$key]['cut'] = cut_str($val['calias'],6);
						$data[$key]['dtime'] = date('m-d H:i',strtotime($val['dtime']));					
					}
					$data[count($data)-1]['totalPage'] = $totalPage;
					$data[count($data)-1]['nowPage']   = $page;
			        $this->ajaxReturn($data,'JSON');
					}
					
			}
           }

	}